# -*- coding: utf-8 -*-
import scrapy

from scrapy_book.items import ScrapyBookItem


class SpiderForCSS(scrapy.Spider):
    name = 'spider_css_jd'

    def start_requests(self):
        for a in range(5):
            url0 = 'https://book.jd.com/booktop/0-0-0.html?category=1713-0-0-0-10002-{}#comfort'.format(a+1)
            yield scrapy.Request(url=url0, callback=self.parse)

    def parse(self, response):
        items = []

        print(response.css('body > div.w.clearfix > div.g-main > div.m.m-list > div > ul li'))
        for book in response.css('body > div.w.clearfix > div.g-main > div.m.m-list > div > ul li'):
            item = ScrapyBookItem()

            item['title'] = book.css('div.p-detail a::text').extract_first().replace('\n', '').strip()

            item['author'] = book.css('div.p-detail > dl:nth-child(2) > dd > a:nth-child(1)::text').extract_first().replace('\n', '').strip()
            #
            # item['pub_date'] = book.css('div:nth-child(6) span::text').extract_first().replace('\n', '').strip()
            #
            item['publisher'] = book.css('div.p-detail > dl:nth-child(3) > dd > a::text').extract_first().replace('\n', '').strip()
            #
            item['price'] = book.css('div.p-detail > dl:nth-child(5) > dd > em::text').extract_first().replace('\n', '').strip()


            #
            # item['score'] = book.css('div.star.clearfix span.rating_nums::text').extract_first().replace('\n',
            #                                                                                              '').strip()
            # item['scrible'] = book.css('p.quote span.inq::text').extract_first().replace('\n', '').strip()
            # item['num'] = book.css('div.star.clearfix span.pl::text').extract_first().strip("(").strip(")").replace(
            #     '\n', '').strip()
            item['img'] = book.css('div.p-img > a > img').xpath('@data-lazy-img').extract_first().replace('\n', '').strip()
            # print(item['img'])

            items.append(item)

        print(items)
        return items
